﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using MisDLL;
using System.Data;

namespace MisBLL
{
    public class CusShopDAO : MarshalByRefObject, ICusShop
    {
        protected DbHelper dbHelper = new DbHelper();
        protected MisEntity.DataEntityDataContext Ctx = new MisEntity.DataEntityDataContext();

        //根据编码查询客户分店代码信息
        public MisModel.CusShop GetById(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            string sql = string.Format("select * from CusShop_TAB where CUS_ID='{0}'", id);
            DataTable dt=  dbHelper.ExecuteDataTable(sql);
            MisModel.CusShop entity = null;
            if (dt != null && dt.Rows.Count > 0){
                entity = new MisModel.CusShop();
                entity.CusId = dt.Rows[0]["CUS_ID"].ToString();
                entity.CusShop_ = dt.Rows[0]["CUS_SHOP"].ToString();
                entity.Shop = dt.Rows[0]["SHOP"].ToString();
                entity.F1 = dt.Rows[0]["F1"].ToString();
                entity.DateE = DateTime.Parse(dt.Rows[0]["DATE_E"].ToString());
            }
            return entity;
        }

        //根据编码查询客户分店代码信息
        public MisModel.CusShop GetBy(string id,string shop)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            string sql = string.Format("select * from CusShop_TAB where CUS_ID='{0}' and CUS_SHOP='{1}'", id,shop);
            DataTable dt = dbHelper.ExecuteDataTable(sql);
            MisModel.CusShop entity = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                entity = new MisModel.CusShop();
                entity.CusId = dt.Rows[0]["CUS_ID"].ToString();
                entity.CusShop_ = dt.Rows[0]["CUS_SHOP"].ToString();
                entity.Shop = dt.Rows[0]["SHOP"].ToString();
                entity.F1 = dt.Rows[0]["F1"].ToString();
                entity.DateE = DateTime.Parse(dt.Rows[0]["DATE_E"].ToString());
            }
            return entity;
        }

        //查询客户分店代码列表
        public DataTable GetAll()
        {
            string sql = "select *,(CUS_ID + '  ' + CUS_SHOP + '      ' + F1+ '  ' + SHOP) as ItemShow  from CusShop_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        //查询客户分店代码列表
        public IList<MisEntity.CusShop> FindAll()
        {
            var query = from item in Ctx.CusShop
                        select item;
            if (query.Count<MisEntity.CusShop>() == 0)
                return null;
            return query.ToList<MisEntity.CusShop>();
        }

        //根据客户ID查询所属分店
        public IList<MisEntity.CusShop> FindBy(string CustID)
        {
            var query = from item in Ctx.CusShop
                        where item.CUS_ID == CustID
                        select item;
            if (query.Count<MisEntity.CusShop>() == 0)
                return null;
            return query.ToList<MisEntity.CusShop>();
        }

        //添加客户分店代码信息
        public int Add(MisModel.CusShop obj)
        {
            if (string.IsNullOrEmpty(obj.F1.ToString()))
            {
                obj.F1 = "Y";
            }
            string sql = string.Format("insert into CusShop_TAB(CUS_ID,CUS_SHOP,SHOP,F1,DATE_E) values('{0}','{1}','{2}','{3}','{4}')", obj.CusId, obj.CusShop_,obj.Shop, obj.F1,obj.DateE);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改客户分店代码信息
        public int Update(MisModel.CusShop obj)
        {
            string sql = string.Format("update CusShop_TAB set SHOP='{0}',F1='{1}',DATE_E='{2}' where CUS_ID='{3}' and CUS_SHOP='{4}'", obj.Shop, obj.F1, obj.DateE, obj.CusId, obj.CusShop_);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除客户分店代码信息
        public int Delete(string id)
        {
            string sql = string.Format("delete from CusShop_TAB where CUS_ID='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }
    }
}
